//
//  FacebookCountTest.m
//  DualMediationSDK
//
//  测试Facebook广告按天计数重置功能
//

#import <Foundation/Foundation.h>
#import "DMRewardedVideoManager.h"
#import "DualMediationMAXRewardedVideoManager.h"
#import "IMLogUtils.h"

@interface FacebookCountTest : NSObject

@end

@implementation FacebookCountTest

/**
 * 测试DMRewardedVideoManager的按天计数功能
 */
+ (void)testDMRewardedVideoManagerDailyReset {
    NSLog(@"🧪 开始测试 DMRewardedVideoManager 按天计数重置功能");
    
    DMRewardedVideoManager *manager = [DMRewardedVideoManager shareRewardedVideoManager];
    
    // 模拟Facebook广告展示事件
    LPMAdInfo *adInfo = [[LPMAdInfo alloc] init];
    // 注意：这里需要根据实际的LPMAdInfo结构来设置adNetwork
    // adInfo.adNetwork = @"Facebook";
    
    // 测试展示计数
    NSLog(@"🧪 测试前展示计数: %ld", (long)[manager getFacebookShowCount]);
    [manager handleFacebookAdControl:adInfo eventType:@"display"];
    NSLog(@"🧪 测试后展示计数: %ld", (long)[manager getFacebookShowCount]);
    
    // 测试点击计数
    NSLog(@"🧪 测试前点击计数: %ld", (long)[manager getFacebookClickCount]);
    [manager handleFacebookAdControl:adInfo eventType:@"click"];
    NSLog(@"🧪 测试后点击计数: %ld", (long)[manager getFacebookClickCount]);
    
    // 测试日期重置逻辑
    NSLog(@"🧪 手动触发日期检查");
    [manager checkAndResetDailyFacebookCounts];
    
    NSLog(@"🧪 DMRewardedVideoManager 测试完成");
}

/**
 * 测试DualMediationMAXRewardedVideoManager的按天计数功能
 */
+ (void)testDualMediationMAXRewardedVideoManagerDailyReset {
    NSLog(@"🧪 开始测试 DualMediationMAXRewardedVideoManager 按天计数重置功能");
    
    DualMediationMAXRewardedVideoManager *manager = [DualMediationMAXRewardedVideoManager shareRewardedVideoManager];
    
    // 模拟Facebook广告展示事件
    MAAd *ad = [[MAAd alloc] init];
    // 注意：这里需要根据实际的MAAd结构来设置网络信息
    // ad.networkName = @"Facebook";
    
    // 测试展示计数
    NSLog(@"🧪 测试前展示计数: %ld", (long)[manager getFacebookShowCount]);
    [manager handleFacebookAdControl:ad eventType:@"display"];
    NSLog(@"🧪 测试后展示计数: %ld", (long)[manager getFacebookShowCount]);
    
    // 测试点击计数
    NSLog(@"🧪 测试前点击计数: %ld", (long)[manager getFacebookClickCount]);
    [manager handleFacebookAdControl:ad eventType:@"click"];
    NSLog(@"🧪 测试后点击计数: %ld", (long)[manager getFacebookClickCount]);
    
    // 测试日期重置逻辑
    NSLog(@"🧪 手动触发日期检查");
    [manager checkAndResetDailyFacebookCounts];
    
    NSLog(@"🧪 DualMediationMAXRewardedVideoManager 测试完成");
}

/**
 * 测试NSUserDefaults存储功能
 */
+ (void)testUserDefaultsStorage {
    NSLog(@"🧪 开始测试 NSUserDefaults 存储功能");
    
    // 清除之前的测试数据
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"DMRewardedVideo_FB_ShowCount"];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"DMRewardedVideo_FB_ClickCount"];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"DMRewardedVideo_FB_LastUpdateDate"];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"DualMediationMAX_FB_ShowCount"];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"DualMediationMAX_FB_ClickCount"];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"DualMediationMAX_FB_LastUpdateDate"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    // 测试数据写入
    [[NSUserDefaults standardUserDefaults] setInteger:5 forKey:@"DMRewardedVideo_FB_ShowCount"];
    [[NSUserDefaults standardUserDefaults] setInteger:2 forKey:@"DMRewardedVideo_FB_ClickCount"];
    [[NSUserDefaults standardUserDefaults] setObject:@"2024-01-15" forKey:@"DMRewardedVideo_FB_LastUpdateDate"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    // 测试数据读取
    NSInteger showCount = [[NSUserDefaults standardUserDefaults] integerForKey:@"DMRewardedVideo_FB_ShowCount"];
    NSInteger clickCount = [[NSUserDefaults standardUserDefaults] integerForKey:@"DMRewardedVideo_FB_ClickCount"];
    NSString *lastDate = [[NSUserDefaults standardUserDefaults] stringForKey:@"DMRewardedVideo_FB_LastUpdateDate"];
    
    NSLog(@"🧪 读取的展示计数: %ld", (long)showCount);
    NSLog(@"🧪 读取的点击计数: %ld", (long)clickCount);
    NSLog(@"🧪 读取的最后更新日期: %@", lastDate);
    
    // 验证数据正确性
    if (showCount == 5 && clickCount == 2 && [lastDate isEqualToString:@"2024-01-15"]) {
        NSLog(@"🧪 ✅ NSUserDefaults 存储测试通过");
    } else {
        NSLog(@"🧪 ❌ NSUserDefaults 存储测试失败");
    }
    
    NSLog(@"🧪 NSUserDefaults 存储功能测试完成");
}

/**
 * 测试日期字符串生成功能
 */
+ (void)testDateStringGeneration {
    NSLog(@"🧪 开始测试日期字符串生成功能");
    
    DMRewardedVideoManager *dmManager = [DMRewardedVideoManager shareRewardedVideoManager];
    DualMediationMAXRewardedVideoManager *maxManager = [DualMediationMAXRewardedVideoManager shareRewardedVideoManager];
    
    NSString *dmDateString = [dmManager getTodayDateString];
    NSString *maxDateString = [maxManager getTodayDateString];
    
    NSLog(@"🧪 DMRewardedVideoManager 日期字符串: %@", dmDateString);
    NSLog(@"🧪 DualMediationMAXRewardedVideoManager 日期字符串: %@", maxDateString);
    
    // 验证日期格式 (YYYY-MM-DD)
    NSRegularExpression *dateRegex = [NSRegularExpression regularExpressionWithPattern:@"^\\d{4}-\\d{2}-\\d{2}$" options:0 error:nil];
    
    if ([dateRegex numberOfMatchesInString:dmDateString options:0 range:NSMakeRange(0, dmDateString.length)] > 0 &&
        [dateRegex numberOfMatchesInString:maxDateString options:0 range:NSMakeRange(0, maxDateString.length)] > 0) {
        NSLog(@"🧪 ✅ 日期字符串格式测试通过");
    } else {
        NSLog(@"🧪 ❌ 日期字符串格式测试失败");
    }
    
    NSLog(@"🧪 日期字符串生成功能测试完成");
}

/**
 * 运行所有测试
 */
+ (void)runAllTests {
    NSLog(@"🧪 ========== Facebook按天计数重置功能测试开始 ==========");
    
    [self testUserDefaultsStorage];
    [self testDateStringGeneration];
    [self testDMRewardedVideoManagerDailyReset];
    [self testDualMediationMAXRewardedVideoManagerDailyReset];
    
    NSLog(@"🧪 ========== Facebook按天计数重置功能测试结束 ==========");
}

@end

// 如果需要在应用启动时运行测试，可以在适当的地方调用：
// [FacebookCountTest runAllTests];